package com.zhongxi.guoyuan.adapter.rewardCoinOrder;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.GridOrderAdapter;
import com.zhongxi.guoyuan.adapter.RewardOrderAdapter;
import com.zhongxi.guoyuan.entity.rewardcoin.MallInfoResult;
import com.zhongxi.guoyuan.entity.rewardcoin.RewardCoinOrderResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.widget.goldshop.NoScrollGridView;
import com.zhongxi.guoyuan.widget.sliding.NoScrollListView;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2016/11/21.
 */

public class RewardOrderHasNoSendAdapter extends BaseAdapter {

    private Context context;
    private List<RewardCoinOrderResult.RewardCoinOrder> list ;
    private LayoutInflater mLayoutInflater;

    private int mExpandedMenuPos = -1;
    private MallInfoResult result;

    public RewardOrderHasNoSendAdapter(Context context, List<RewardCoinOrderResult.RewardCoinOrder> data) {
        this.context = context;
        this.mLayoutInflater = LayoutInflater.from(context);
        this.list = data;
    }

    public int getmExpandedMenuPos() {
        return mExpandedMenuPos;
    }

    public void setmExpandedMenuPos(int mExpandedMenuPos) {
        this.mExpandedMenuPos = mExpandedMenuPos;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mLayoutInflater.inflate(R.layout.item_reward_order, parent, false);
                holder.tvNum = (TextView) convertView.findViewById(R.id.tvNum);
                holder.tvMoney = (TextView) convertView.findViewById(R.id.tvMoney);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.gvList = (NoScrollGridView) convertView.findViewById(R.id.gvList);
                holder.ivSingleImg = (View) convertView.findViewById(R.id.ivSingleImg);
                holder.gvImg = (View) convertView.findViewById(R.id.gvImg);
                holder.tvType = (TextView) convertView.findViewById(R.id.tvType);
                holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
                holder.tvOrder = (TextView) convertView.findViewById(R.id.tvOrder);
                holder.tvAllCash = (TextView) convertView.findViewById(R.id.tvAllCash);
                holder.tvSend = (TextView) convertView.findViewById(R.id.tvSend);
                holder.tvReward = (TextView) convertView.findViewById(R.id.tvReward);
                holder.tvTips = (TextView) convertView.findViewById(R.id.tvTips);
                holder.tvAllApply = (TextView) convertView.findViewById(R.id.tvAllApply);
                holder.lvList = (NoScrollListView) convertView.findViewById(R.id.lvList);
                holder.ivDown = (ImageView) convertView.findViewById(R.id.iv_down);
                holder.listMenu = (View) convertView.findViewById(R.id.listMenu);
                holder.ivCall = (ImageView) convertView.findViewById(R.id.ivCall);
                holder.relaMyInfo = (RelativeLayout) convertView.findViewById(R.id.relaMyInfo);
                holder.relaSeller = (RelativeLayout) convertView.findViewById(R.id.relaSeller);

                holder.tvOper = (TextView) convertView.findViewById(R.id.tvOper);
                holder.tv = (TextView) convertView.findViewById(R.id.tv);
                //
                holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                holder.tvPhone = (TextView) convertView.findViewById(R.id.tvPhone);
                holder.tvAddress = (TextView) convertView.findViewById(R.id.tvAddress);
                holder.tvStoreName = (TextView) convertView.findViewById(R.id.tvStoreName);
                holder.tvTel = (TextView) convertView.findViewById(R.id.tvTel);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
//            holder.tvOper.setText("待发货");
            holder.tv.setVisibility(View.GONE);
            holder.tvTips.setText("取货信息：");
            holder.tvType.setText("取货方式：邮寄");
            holder.tvAllApply.setText("商家正在努力发货中");
            holder.tvAllApply.setGravity(Gravity.RIGHT);
            /*if (null != info && !"".equals(info)) {
                List<String> imgList = JsonUtils.getData(info.getJSONObject(position).getJSONArray("thumbnail"));
                if (imgList.size() == 1) {
                    holder.ivSingleImg.setVisibility(View.VISIBLE);
                    holder.gvImg.setVisibility(View.GONE);
                    Glide.with(context).load(Url.IMG_URL + imgList.get(0)).placeholder(R.mipmap.iv_eg).fitCenter().into(holder.ivImg);
                } else {
                    holder.ivSingleImg.setVisibility(View.GONE);
                    holder.gvImg.setVisibility(View.VISIBLE);
                    holder.gvList.setAdapter(new GridOrderAdapter(context, imgList));
                }
                if (imgList.size() > 0) {
                    List<String> goodsid = JsonUtils.getData(info.getJSONObject(position).getJSONArray("goodsid"));
                    holder.lvList.setAdapter(new RewardOrderAdapter(context, goodsid, info, position));
                }
            }*/

            if(null!=list.get(position)&&!"".equals(list.get(position))){
                RewardCoinOrderResult.RewardCoinOrder data = list.get(position);
                if(data.getImg().size()>0){
                    if(data.getImg().size() == 1){
                        holder.ivSingleImg.setVisibility(View.VISIBLE);
                        holder.gvImg.setVisibility(View.GONE);
                        Glide.with(context).load(Url.IMG_URL+data.getImg().get(0)).placeholder(R.mipmap.iv_eg).fitCenter().into(holder.ivImg);
                    }else{
                        holder.ivSingleImg.setVisibility(View.GONE);
                        holder.gvImg.setVisibility(View.VISIBLE);
                        holder.gvList.setAdapter(new GridOrderAdapter(context, data.getImg()));
                    }
                }
                if(data.getGoodsId().size()>0){
                    holder.lvList.setAdapter(new RewardOrderAdapter(context, data, position));
                }
                if(null!=data.getOrderid()&&!"".equals(data.getOrderid())){
                    holder.tvOrder.setText("订单号："+data.getOrderid());
                }else{
                    holder.tvOrder.setText("订单号：**");
                }
                if(null!=data.getOrderInfo()&&!"".equals(data.getOrderInfo())){
                    RewardCoinOrderResult.OrderInfo orderInfo = data.getOrderInfo();
                    if(null!=orderInfo.getCtime()&&!"".equals(orderInfo.getCtime())){
                        holder.tvTime.setText("下单时间："+orderInfo.getCtime());
                    }else{
                        holder.tvTime.setText("下单时间：**");
                    }
                    if(null!=orderInfo.getGoodsnum()&&!"".equals(orderInfo.getGoodsnum())){
                        holder.tvNum.setText("共计"+orderInfo.getGoodsnum()+"件");
                    }else{
                        holder.tvNum.setText("共计**件");
                    }
                    if(null!=orderInfo.getPoststatus()&&!"".equals(orderInfo.getPoststatus())){
                        if("1".equals(orderInfo.getPoststatus())){
                            holder.tvOper.setText("正在派件");
                        }else{
                            holder.tvOper.setText("待发货");
                        }
                    }
                    if(null!=orderInfo.getShouldprice()&&!"".equals(orderInfo.getShouldprice())){
                        BigDecimal price = new BigDecimal(orderInfo.getShouldprice());
                        String postage = "0";
                        if(null!=orderInfo.getPostage()&&!"".equals(orderInfo.getPostage())){
                            postage = orderInfo.getPostage();
                        }
                        BigDecimal totalFee = price.add(new BigDecimal(postage));
                        holder.tvMoney.setText("订单总额："+totalFee.toString()+"元[返银币"+price.intValue()+"]");
                        holder.tvAllCash.setText("商品合计："+price+"元");
                        holder.tvSend.setText("快递："+postage+"元");
                        holder.tvReward.setText("返银:"+totalFee.intValue()+"银币");
                    }else{
                        holder.tvMoney.setText("订单总额：**元[返银币**银币]");
                        holder.tvAllCash.setText("商品合计：**元");
                        holder.tvSend.setText("快递：**元");
                        holder.tvReward.setText("返银：**银币");
                    }
                    if(null!=orderInfo.getConsignee()&&!"".equals(orderInfo.getConsignee())){
                        holder.tvName.setText(orderInfo.getConsignee());
                    }else{
                        holder.tvName.setText("**");
                    }
                    if(null!=orderInfo.getConsigneemphone()&&!"".equals(orderInfo.getConsigneemphone())){
                        holder.tvPhone.setText(orderInfo.getConsigneemphone());
                    }else{
                        holder.tvPhone.setText("**");
                    }
                    if(null!=orderInfo.getShippingaddress()&&!"".equals(orderInfo.getShippingaddress())){
                        holder.tvAddress.setText(orderInfo.getShippingaddress());
                    }else{
                        holder.tvAddress.setText("**");
                    }
                }
            }

            final String phone = holder.tvPhone.getText().toString().trim();
            holder.ivCall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + phone);
                    intent.setData(data);
                    context.startActivity(intent);
                }
            });
            holder.ivDown.setImageResource(position == mExpandedMenuPos ? R.mipmap.iv_down_light : R.mipmap.iv_down);
            holder.listMenu.setVisibility(position == mExpandedMenuPos ? View.VISIBLE : View.GONE);

            if(null!=result&&!"".equals(result)){
                if(null!=result.getData()&&!"".equals(result.getData())){
                    MallInfoResult.MallInfo storeInfo = result.getData();
                    if(null!=storeInfo.getName()&&!"".equals(storeInfo.getName())){
                        holder.tvStoreName.setText(storeInfo.getName());
                    }else{
                        holder.tvStoreName.setText(R.string.app_name);
                    }
                    if(null!=storeInfo.getMphone()&&!"".equals(storeInfo.getMphone())){
                        holder.tvTel.setText(storeInfo.getMphone());
                    }else{
                        holder.tvTel.setText("**");
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    public void setMall(MallInfoResult result) {
        this.result = result;
    }

    class ViewHolder {
        private TextView tvNum;
        private TextView tvMoney;
        private TextView tvType;
        private TextView tvTime;
        private TextView tvOrder;
        private TextView tvAllCash;
        private TextView tvSend;
        private TextView tvReward;
        private TextView tvAllApply;
        private TextView tvName;
        private TextView tvTel;
        private TextView tvPhone;
        private TextView tvAddress;
        private TextView tvStoreName;

        private ImageView ivImg;
        private NoScrollGridView gvList;
        private View ivSingleImg;
        private View gvImg;

        private NoScrollListView lvList;
        private ImageView ivDown;
        private View listMenu;
        private ImageView ivCall;
        private TextView tvOper;
        private TextView tv;
        private TextView tvTips;

        private RelativeLayout relaMyInfo;
        private RelativeLayout relaSeller;
    }
}
